//
//  NavScrollView.m
//  ios_news
//
//  Created by Han on 14-6-9.
//  Copyright (c) 2014年 ucoder. All rights reserved.
//

#import "MarketNavScrollView.h"

@implementation MarketNavScrollView
@synthesize mCurrentSelectedID, delegateNews;

static int tagValue = 600;

+ (int)getViewTag
{
    return tagValue;
}


- (id) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.showsHorizontalScrollIndicator = NO;
        //self.backgroundColor = MAIN_COLOR;
        
        [self drawView];
    }
    
    return self;
}


- (void) drawView
{
    
    for (int i = 0; i < [MARKET_CN_ARRAY count]; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle: [MARKET_CN_ARRAY objectAtIndex:i] forState:UIControlStateNormal];
        [button setTag:i + tagValue];
        // 平均五等分
        button.frame = CGRectMake(80 * i, 5, 80, 20);
        
        if (i == 0) {
            button.selected = YES;
            mCurrentSelectedID = i + tagValue;
            mLastSelectedID = i + tagValue;
        }
        
        button.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [button setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal];
        [button setTitleColor:MAIN_COLOR forState:UIControlStateSelected];
        
        [button addTarget:self action:@selector(selectedButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:button];
    }
    
    
    // 必须给出内容的实际大小，滚动视图才知道滚动的范围
    self.contentSize = CGSizeMake(80 * [MARKET_CN_ARRAY count], 30);
    mIndicatorView = [[UIView alloc] initWithFrame:CGRectMake(23, 28, 34, 2)];
    mIndicatorView.backgroundColor = MAIN_COLOR;
    [self addSubview:mIndicatorView];
}

- (void) selectedButton:(UIButton *) button
{
    if (button.selected == YES) {
        return;
    }
    
    UIButton *lastSelectecButton = (UIButton *) [self viewWithTag:mCurrentSelectedID];
    lastSelectecButton.selected = NO;
    
    mCurrentSelectedID = button.tag;
    
    button.selected = YES;
    
    [self.delegateNews newsScrollViewSetContentOffset:mCurrentSelectedID - tagValue];
    [self.delegateNews fristLoadData];

    
    mLastSelectedID = mCurrentSelectedID;
}


- (void) changeIndicatorViewContentOffset: (float) x
{
    [mIndicatorView setFrame:CGRectMake(80 * x + 23, 28, 34, 2)];
}


- (void) adjustNavScrollView: (NSInteger) currentSelectedID
{
    
    if (currentSelectedID == mCurrentSelectedID) {
        return;
    }
    
    mCurrentSelectedID = currentSelectedID;
    
    UIButton *lastSelectButton = (UIButton *) [self viewWithTag:mLastSelectedID];
    
    //NSLog(@"mLastSelectedID=%li", (long)mLastSelectedID);
    //NSLog(@"mCurrentSelectedID=%li", (long)mCurrentSelectedID);
    
    UIButton *currentSelectButton = (UIButton *) [self viewWithTag:mCurrentSelectedID];
    
    
    lastSelectButton.selected = NO;
    currentSelectButton.selected = YES;
    
    
    mLastSelectedID = mCurrentSelectedID;
}


@end
